package com.qianbao.qianbaobusiness.ui.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.qianbao.qianbaobusiness.R;

/**
 * @author: Routee
 * @date 2018/6/12
 * @mail wangc4@qianbaocard.com
 * ------------1.本类由Routee开发,阅读、修改时请勿随意修改代码排版格式后提交到git。
 * ------------2.阅读本类时，发现不合理请及时指正.
 * ------------3.如需在本类内部进行修改,请先联系Routee,若未经同意修改此类后造成损失本人概不负责。
 */

public class ConfirmDialog extends Dialog {
    public ConfirmDialog(@NonNull Context context) {
        super(context);
    }

    public ConfirmDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected ConfirmDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public static class Builder {
        private final  Context              context;
        private static String               mContentText;
        private        int                  mContentColor;
        private        String               mNegativeText;
        private        int                  mNegativeColor;
        private        String               mPositiveText;
        private        int                  mPositiveColor;
        private        TextView             mTvContent;
        private        TextView             mTvPositive;
        private        TextView             mTvNegative;
        private        boolean              mCancelable;
        private        ConfirmClickListener mListener;

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setContentText(String contentText) {
            mContentText = contentText;
            return this;
        }

        public Builder setContentText(String contentText, int contentColor) {
            mContentColor = contentColor;
            return this.setContentText(contentText);
        }

        public Builder setNegativeText(String negativeText) {
            mNegativeText = negativeText;
            return this;
        }

        public Builder setNegativeText(String negativeText, int negativeColor) {
            mNegativeColor = negativeColor;
            return this.setNegativeText(negativeText);
        }

        public Builder setPositiveText(String positiveText) {
            mPositiveText = positiveText;
            return this;
        }

        public Builder setPositiveText(String positiveText, int positiveColor) {
            mPositiveColor = positiveColor;
            return this.setPositiveText(positiveText);
        }

        public Builder setCanceledOnTouchOutside(boolean b) {
            mCancelable = b;
            return this;
        }

        public ConfirmDialog create() {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final ConfirmDialog dialog = new ConfirmDialog(context, R.style.customDialogTheme);
            View layout = inflater.inflate(R.layout.dialog_confirm, null);
            dialog.addContentView(layout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            mTvContent = layout.findViewById(R.id.tv_content);
            mTvPositive = layout.findViewById(R.id.tv_positive);
            mTvNegative = layout.findViewById(R.id.tv_negative);
            if (!TextUtils.isEmpty(mContentText)) {
                mTvContent.setText(mContentText);
            }
            if (0 != mContentColor) {
                mTvContent.setTextColor(ContextCompat.getColor(context, mContentColor));
            }
            if (!TextUtils.isEmpty(mPositiveText)) {
                mTvPositive.setText(mPositiveText);
            }
            if (0 != mPositiveColor) {
                mTvPositive.setTextColor(ContextCompat.getColor(context, mPositiveColor));
            }
            if (!TextUtils.isEmpty(mNegativeText)) {
                mTvNegative.setText(mNegativeText);
            }
            if (0 != mNegativeColor) {
                mTvNegative.setTextColor(ContextCompat.getColor(context, mNegativeColor));
            }
            mTvPositive.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mListener != null) {
                        mListener.click(true);
                    }
                }
            });
            mTvNegative.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mListener != null) {
                        mListener.click(false);
                    }
                }
            });
            dialog.setCanceledOnTouchOutside(mCancelable);
            return dialog;
        }

        public interface ConfirmClickListener {
            void click(boolean b);
        }

        public Builder setConfirmListener(ConfirmClickListener listener) {
            mListener = listener;
            return this;
        }
    }
}

